import axios from "axios";
import { Message } from "@arco-design/web-vue";
const myAxios = axios.create({
  baseURL: "http://localhost:8101/",
  timeout: 30000,
  withCredentials: true, //cookie
});

// Global request interceptor
myAxios.interceptors.request.use(
  function (config) {
    // What to do before sending the request
    return config;
  },
  function (error) {
    // What to do about request errors
    return Promise.reject(error);
  }
);

// Global response interceptor
myAxios.interceptors.response.use(
  function (response) {
    console.log(response);
    const { data } = response;

    //Not logged in
    if (data.code === 40100) {
      if (
        !response.request.responseURL.includes("/user/get/login") &&
        !window.location.pathname.includes("/user/login")
      ) {
        Message.warning("Please log in first");
        window.location.href = "/user/login?redirect=${window.location.href}";
      }
    }
    return response;
  },
  function (error) {
    return Promise.reject(error);
  }
);

export default myAxios;
